#include "TestSegment.h"
#include <iostream>

using namespace std;

CTestSegment::CTestSegment(	 EIOType	eIoType,
							 DWORD		dwSegmentBytes,
							 DWORDLONG	dwlOffset):	 m_eIoType(eIoType),
													 m_dwSegmentBytes(dwSegmentBytes),
													 m_dwlOffset(dwlOffset),
													 m_psNextSegment(NULL),
													 m_bIsReady(false),
													 m_pBuffer(NULL)
							 

{


}

CTestSegment::~CTestSegment(void)
{
	// Free the allocated buffer
	if (m_pBuffer != NULL)
	{
		VirtualFree(m_pBuffer, 0, MEM_RELEASE);
	}
}

bool CTestSegment::PrepareTest(HANDLE	hDiskFile)
{

	// Create an event.
	m_oOverlapInfo.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	// Was the event created successfully?
	if (m_oOverlapInfo.hEvent == NULL) {
		cout << "*** CreateEvent() failed in CTestSegment::PrepareTest()" << endl;
		m_bIsReady = false;
	}
	else
	{
		// Set the low-order bit of the event handle to prevent it from being
		// picked up by a later call to GetQueuedCompletionStatus() 
		m_oOverlapInfo.hEvent = (HANDLE) ((UINT_PTR) m_oOverlapInfo.hEvent | 0x0000000000000001);

		// Set its address. the offset is inside the file - for example if you want offset 10 
		// so the written file size will be the offset + written buffer size

		m_oOverlapInfo.Offset		= (DWORD)m_dwlOffset;
		m_oOverlapInfo.OffsetHigh	= (DWORD)(m_dwlOffset >> 32);

		// Allocate a buffer
		VirtualFree(m_pBuffer, 0, MEM_RELEASE);
		if (!(m_pBuffer = VirtualAlloc(NULL, m_dwSegmentBytes, MEM_COMMIT, PAGE_READWRITE)))
		{
			cout << "*** Could not allocate buffer to prepare disk." << endl;
			m_bIsReady =  false;
		}
		else
		{
			m_bIsReady = true;
		}

	}
	return m_bIsReady;
}
